package TwoSeach;

import org.junit.Test;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: TwoSeach
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-11-05  09:12
 * @Description: TODO
 * @Version: 1.0
 */
public class 搜索旋转排序数组33 {

    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        return searchHelp(nums, left, right, target);
    }

    private int searchHelp(int[] nums, int left, int right, int target) {
        if (left > right) {
            return -1;
        }
        int mid = left + (right - left) / 2;
        int leftValue = nums[left];
        int midValue = nums[mid];
        int rightValue = nums[right];

        if (leftValue == target) {
            return left;
        }
        if (midValue == target) {
            return mid;
        }
        if (rightValue == target) {
            return right;
        }
        // 比较端点数据
        // 左边有序
        if (leftValue < midValue) {
            if (target > leftValue && target < midValue) {
                // 选择左边的数组
                return searchHelp(nums, left + 1, mid - 1, target);
            } else {
                // 选择右边的数组
                return searchHelp(nums, mid + 1, right - 1, target);
            }
        } else {
            // 右边有序
            if (target > midValue && target < rightValue) {
                // 选择右边的开始数组比较
                return searchHelp(nums, mid + 1, right - 1, target);
            } else {
                // 否者选择左边
                return searchHelp(nums, left + 1, mid - 1, target);
            }
        }
    }

    @Test
    public void test() {
        int search = search(new int[]{4,5,6,7,0,1,2}, 0);

    }
}
